import Utils from "../../../Utils";
import IntercontinentalMissile from "../../flyer/DestructibleFlyer/FlyerMissile/IntercontinentalMissile";
import { Killer } from "../../funcObj/interface/HealthPoint";
import GameManager from "../../manager/GameManager";
import { Location } from "../../info/Predefine";
import NodePoolManager from "../../manager/NodePoolManager";
import Player from "../../player/Player";
import LaunchableFalicity from "../LaunchableFalicity";

const { ccclass, property } = cc._decorator;

/**
 * 洲际导弹基地
 */
@ccclass
export default class IntercontinentalMissileBase extends LaunchableFalicity {

    @property(cc.Node)
    points: cc.Node = null;

    intercontinentalMissileQueue: Array<cc.Node>;

    creating: boolean;

    get count(): number {
        return this.intercontinentalMissileQueue.length;
    }

    init(player: Player): void {
        super.init(player);
        this.creating = false;
        this.intercontinentalMissileQueue = [];
        this.creat();
    }

    creat() {
        if (this.creating) return;
        this.CreatingProgress.progress = 0;
        this.CreatingProgress.node.active = true;
        this.creating = true;
        cc.tween(this.CreatingProgress)
            .to(this.makingTime, { progress: 1 })
            .call(() => {
                let creatMissile = NodePoolManager.instance.obtain(NodePoolManager.instance.intercontinentalMissileBullet);
                creatMissile.getComponent(IntercontinentalMissile).init(this);
                this.intercontinentalMissileQueue.push(creatMissile);
                let flyerParent = this.player.flyerParentNode;
                creatMissile.parent = flyerParent;
                creatMissile.position = flyerParent.convertToNodeSpaceAR(this.points.convertToWorldSpaceAR(cc.Vec3.ZERO));
                this.player.node.emit(Player.EventType.更新飞行物数量, this.facilityType, 1);
            }).call(() => {
                this.CreatingProgress.node.active = false;
                this.creating = false;
                if (this.intercontinentalMissileQueue.length < this.facilityInfo.每基地可制造上限) {
                    this.creat();
                }
            })
            .start();
    }

    dispatch(target: Location) {
        super.dispatch(target);
        let intercontinentalMissile: cc.Node = this.intercontinentalMissileQueue.shift();
        intercontinentalMissile.getComponent(IntercontinentalMissile).dispatch(target);
        this.player.node.emit(Player.EventType.更新飞行物数量, this.facilityType, -1);
        this.dispatchQueue.push({flyer : intercontinentalMissile , target : target})
        this.creat();
    }

    die(killer: Killer): void {
        super.die(killer);
        this.intercontinentalMissileQueue.forEach(intercontinentalMissile => NodePoolManager.instance.recycle(intercontinentalMissile));
        this.intercontinentalMissileQueue = null;
    }
}
